﻿package com.cloud.core.adapter;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * 1Cloud1
 * @author Administrator
 * @since 2015-11-5
 * @version 1.0
 */
@Component("MvcInterceptor")
public class MvcInterceptor extends HandlerInterceptorAdapter {
	private static final Logger logger = LoggerFactory.getLogger(MvcInterceptor.class);
	@Value("${cloud.request.md5}")
	public String isMd5;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		logger.debug(String.format("请求的URL：Method[%s], RequestURI[%s], ContextPath[%s]", request.getMethod(), request.getRequestURI(), request.getContextPath()));
		if(Boolean.valueOf(isMd5)){
			PrintWriter out = response.getWriter();
			out.print("Error.Reason:" + isMd5);
			return false;
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { }

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { }
}
